package com.jeesite.modules.dataScreen.supports;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.jeesite.modules.dataScreen.web.vo.IndustryIndexVo;
import com.jeesite.modules.dataScreen.web.vo.perception.WarningNewModelVo;
import com.jeesite.modules.dataScreen.web.vo.perception.WarningNewRunwayVo;
import com.jeesite.modules.dataScreen.web.vo.perception.WarningNewSubRunwayVo;

import java.util.*;
import java.util.stream.Collectors;

public final class WarningNewModelVoSupport {

    public static List<WarningNewModelVo> fitTogether(List<IndustryIndexVo> industryIndexVos, String... modelNames) {
        if (modelNames.length == 0 || CollUtil.isEmpty(industryIndexVos)) {
            return Collections.emptyList();
        }
        Map<String, List<IndustryIndexVo>> listMap = industryIndexVos.stream().collect(Collectors.groupingBy(IndustryIndexVo::getPlate));
        List<WarningNewModelVo> result = new ArrayList<>();
        for (String modelName : modelNames) {
            result.add(assemblyModel(modelName, listMap));
        }
        return result;
    }

    private static WarningNewModelVo assemblyModel(String modelName, Map<String, List<IndustryIndexVo>> listMap) {
        WarningNewModelVo result = new WarningNewModelVo();
        result.setTitle(modelName);
        List<IndustryIndexVo> industryIndexVos = listMap.get(modelName);
        List<WarningNewRunwayVo> children = new ArrayList<>();
        if (CollUtil.isNotEmpty(industryIndexVos)) {
            //跑道
            WarningNewRunwayVo runwayVo;
            Map<String, List<IndustryIndexVo>> pdMap = industryIndexVos.stream().collect(Collectors.groupingBy(IndustryIndexVo::getRunway));
            for (Map.Entry<String, List<IndustryIndexVo>> entry : pdMap.entrySet()) {
                runwayVo = new WarningNewRunwayVo();
                runwayVo.setTitle(entry.getKey());
                List<IndustryIndexVo> value = entry.getValue();
                //子跑道
                Map<String, List<IndustryIndexVo>> subRunwayMap = value.stream().collect(Collectors.groupingBy(IndustryIndexVo::getSubRunway));
                List<WarningNewSubRunwayVo> subChildren = new ArrayList<>();
                WarningNewSubRunwayVo subRunwayVo;
                for (Map.Entry<String, List<IndustryIndexVo>> listEntry : subRunwayMap.entrySet()) {
                    subRunwayVo = new WarningNewSubRunwayVo();
                    //指标数据
                    List<IndustryIndexVo> subValues = listEntry.getValue();
                    for (IndustryIndexVo subValue : subValues) {
//                        String color = IndustryIndexVo.color(subValue);
                        String[] strings = IndustryIndexVo.titleAndColor(subValue);
                        if (strings != null) {
                            subValue.setColor(strings[1]);
                            subValue.setWarningTitle(strings[0]);
                        }
                    }
                    subValues.sort(Comparator.comparing(IndustryIndexVo::getIndicatorName));
                    subRunwayVo.setIndexVos(subValues);
                    //获取第一个预警得指标
                    List<IndustryIndexVo> indexVos = subValues.stream().filter(e -> StrUtil.isNotBlank(e.getColor())).collect(Collectors.toList());
                    if (CollUtil.isNotEmpty(indexVos)) {
                        IndustryIndexVo vo = indexVos.get(0);
                        subRunwayVo.setWarningIndexVo(vo);
                        subRunwayVo.setColor(vo.getColor());
                    }
                    subRunwayVo.setWarningNum(indexVos.size());
                    subRunwayVo.setTitle(listEntry.getKey());
                    subRunwayVo.setNum(subValues.size());
                    subChildren.add(subRunwayVo);
                }
                subChildren.sort(Comparator.comparing(WarningNewSubRunwayVo::getTitle));
                runwayVo.setChildren(subChildren);
                runwayVo.setNum(value.size());
                int warningNum = 0;
                if (CollUtil.isNotEmpty(subChildren)) {
                    Optional<WarningNewSubRunwayVo> first = subChildren.stream().filter(e -> Objects.nonNull(e.getWarningIndexVo())).findFirst();
                    if (first.isPresent()) {
                        WarningNewSubRunwayVo vo = first.get();
                        runwayVo.setWarningIndexVo(vo.getWarningIndexVo());
                        runwayVo.setColor(vo.getColor());
                    }
                    warningNum = subChildren.stream().mapToInt(WarningNewSubRunwayVo::getWarningNum).sum();
                }
                runwayVo.setWarningNum(warningNum);
                children.add(runwayVo);
            }
            //设置每个模块默认显示指标
            Optional<IndustryIndexVo> first = industryIndexVos.stream().sorted(Comparator.comparing(IndustryIndexVo::getIndicatorName)).filter(e -> StrUtil.isNotBlank(e.getIndicatorValue())).findFirst();
            first.ifPresent(result::setIndexVo);
        }
        children.sort(Comparator.comparing(WarningNewRunwayVo::getTitle));
        result.setChildren(children);
        result.setWarningNum(0);
        if (CollUtil.isNotEmpty(children)) {
            Optional<WarningNewRunwayVo> first = children.stream().filter(e -> Objects.nonNull(e.getWarningIndexVo())).findFirst();
            if (first.isPresent()) {
                WarningNewRunwayVo runwayVo = first.get();
                result.setIndexVo(runwayVo.getWarningIndexVo());
            }
            int sum = children.stream().mapToInt(WarningNewRunwayVo::getWarningNum).sum();
            result.setWarningNum(sum);
        }
        return result;
    }

}
